﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckNoMaintenancePlanWithReorganizeIndex : IssueTypeCheckBase
    {

        public ServiceCheckNoMaintenancePlanWithReorganizeIndex()
        {
            IssueTypeId = 2013;
        }

        public override void Check()
        {
            try
            {
                
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                // Query for maintenance plans with reorganize index enabled
                var qryin = (from s in dc.Services
                          from m in s.MaintenanceSubPlans
                                     where m.HasReorganizeIndexTask == true
                                    
                                     select new{s.ServiceID}).Distinct();
                var Qry = from ss in dc.Services
                           where ss.ProductVersion != null
                          select new {ss.ServiceID};

                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);
               
             
            
                foreach (var service in Qry.Except(qryin))
	{
		 
	
                            Issue issue = new Issue();
                            issue.IssueType = issueType;
                            issue.TargetId =service.ServiceID;
                            issue.Description = issueType.Description;
                            dc.Issues.InsertOnSubmit(issue);
                            dc.SubmitChanges();
                  
                  

                }
                
            
                

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
}
